<template>
  <div class="app-container clearfix">  
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="filterForm.keyword" clearable placeholder="合约" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="上市时间" prop="marketDate">
          <el-date-picker 
          clearable
            v-model="filterForm.regio"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <el-button @click="handleFilter()">搜索</el-button>
      <el-button @click="handleCreate()">新建</el-button>
    </div>
    <el-tag
      v-for="(mineral,index) in historyMineralList"
      :key="mineral.ID"
      style="margin-right:10px;"
      @close="handleBreadRemove(mineral,index)"
      closable
      >
      {{mineral.mineralName}}
    </el-tag>
      <el-table
      :data="list"
      border
      style="width: 100%">
      <el-table-column
      type="selection"
      width="55">
    </el-table-column>
      <el-table-column
        label="标题">
        <template slot-scope="{row}">
          <div @click="handleMineralList(row)">{{ row.mineralName }}</div>
          <div>{{ row.mineralCode }} <i class="el-icon-copy-document"></i></div>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.mineralStatus == 1" type="danger" effect="dark">待分配</el-tag>
          <el-tag v-else-if="row.mineralStatus == 2" type="warning" effect="dark">待处理</el-tag>
          <el-tag v-else-if="row.mineralStatus == 3" type="info" effect="dark">已作废</el-tag>
          <el-tag v-else-if="row.mineralStatus == 4" effect="dark">已添加</el-tag>
          <el-tag v-else effect="dark">未定义</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="排序" align="center" width="240">
        <template slot-scope="{row}">
            <el-input-number v-model="row.mineralIndex" :step="2"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column
        label="最后修改"
        width="160">
        <template slot-scope="{row}">
          <span>{{ row.updatedAt }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="100">
        <template slot-scope="{row}">
          <el-button @click="handleCreate(row)">新建</el-button>
        </template>
      </el-table-column>
      </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="filterForm.page" :limit.sync="filterForm.size" @pagination="getList" />
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getMineralList} from '@/api/geography/mineral'


export default {
  name: 'mineralAdmin',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      list:[],
      historyMineralList:[],
      filterForm: {
        page: 1,
        size: 10,
        parentMineralID:0
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList:function(){
      let _this = this;
      getMineralList(_this.filterForm).then((response) => {
        console.log("getList",response)
          if (response.code === 200) {
            this.list = response.data.list
            this.total = response.data.total
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    handleBreadRemove:function(mineral,selectedIndex){
      let _this = this
      let removeMineralIDs = new Array()
      _this.historyMineralList.forEach(function(item,index){
        if(index >= selectedIndex){
          removeMineralIDs.push(item['ID'])
        }
      })
      removeMineralIDs.forEach(function(removeMineralID){
        _this.historyMineralList.forEach(function(item,index){
          if(item['ID'] == removeMineralID){
            _this.historyMineralList.splice(index,1)
          }
        })
      })
      let selectedTotal = _this.historyMineralList.length
      if(selectedTotal){
        let mineralData = _this.historyMineralList[_this.historyMineralList.length-1]
        _this.filterForm.parentMineralID = mineralData['ID']
      }else{
        _this.filterForm.parentMineralID = 0

      }
      _this.getList()
    },
    handleMineralList:function(mineral){
      this.filterForm.parentMineralID = mineral.ID
      this.historyMineralList.push(mineral)
      this.getList()
    },
    handleCreate:function(row){
      if(row == undefined){
        if(this.filterForm.parentMineralID){
          this.$router.push({ path: 'mineral-add', query: { 'parentMineralID': this.filterForm.parentMineralID }})
        }else{
          this.$router.push({ path: 'mineral-add'})
        }
      }else{
        this.$router.push({ path: 'mineral-add', query: { 'parentMineralID': row.ID }})
      }
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    },
    sortChange:function(){

    }
  }
}
</script>
<style>
.filter { float: left; width: 160px; padding: 20px; min-height: 600px; }
.article { float: left; width: 800px; }
.article-item { width: 800px; height: 200px; margin-right: 25px; }
.article-item-text { float: left; width:600px; }
.article-item-icon { float: right; width:200px; height:150px; padding-left: 50px; }
.article-item-icon img { width:100%; height:100%; border-radius: 10px; }
</style>